package ui.fragment.minefragment;

import android.app.Dialog;
import android.support.v4.app.Fragment;
import android.view.View;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.Switch;
import android.widget.TextView;

import java.util.HashMap;
import java.util.Map;

import Dailog.CommomDialog;
import base.BaseBean;
import base.fragment.BaseNetFragment;
import bean.LoginBean;
import student.andlei.com.student.R;
import ui.activity.login.LoginActivity;
import ui.fragment.MineFragment;
import utils.CleanMessageUtils;
import utils.SPUtils;

/**
 *设置
 */
public class SettingFragment extends BaseNetFragment{
    private TextView tv_updatepd,tv_clean;
    private RelativeLayout rly_clean;
    private TextView tv_appversion;
    private Button bt;
    public static Fragment getInstance() {
        return new SettingFragment();
    }
    @Override
    public void success(String action, BaseBean baseBean) {
        if(baseBean.code == 200){
            jumpTo(LoginActivity.class);
            getActivity().finish();
        }else {
            toast(""+baseBean.msg);
        }
    }

    @Override
    public void error(String action, Throwable e) {

    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_setting;
    }

    @Override
    protected void findViews() {
        bt = getView(R.id.bt_loginout);
        tv_updatepd = getView(R.id.tv_setting_updatepassword);
        rly_clean = getView(R.id.Rly_clean);
        tv_clean = getView(R.id.tv_clean);
        tv_appversion = getView(R.id.tv_appVersion);
    }

    @Override
    protected void formatViews() {
        bt.setOnClickListener(this);
        rly_clean.setOnClickListener(this);
        tv_updatepd.setOnClickListener(this);
    }

    @Override
    protected void formatData() {
        tv_appversion.setText("V"+SPUtils.getInstance(context).getLocalVersionName(context));
        try {
            tv_clean.setText(CleanMessageUtils.getTotalCacheSize(getActivity()));
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    @Override
    protected void getBundle() {

    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            //退出登录 ，清空token 和 id
            case R.id.bt_loginout:
                SPUtils.getInstance(context).put("token","");
                SPUtils.getInstance(context).put("student_id","");
                jumpTo(LoginActivity.class);
                getActivity().finish();
//                Map<String,String> map = new HashMap<>();
//                post("common/logout",map, LoginBean.class,true);
                break;
            case R.id.tv_setting_updatepassword:
                ((MineFragment)(this.getParentFragment())).GojumpUpdateFragment();
                break;
            case R.id.Rly_clean:
                new CommomDialog(getActivity(), R.style.dialog, "是否确认清除缓存？", new CommomDialog.OnCloseListener() {
                    @Override
                    public void onClick(Dialog dialog, boolean confirm) {
                        if (confirm) {
                            CleanMessageUtils.clearAllCache(getActivity());
                            tv_clean.setText("0kB");
                            dialog.dismiss();
                        }
                    }
                }).setTitle("提示").settPositiveButtonColor(R.color.homepage_titlefont).show();
                break;
        }
    }
}
